package org.grow.loginto.controller;

import org.grow.loginto.dao.UserInfoDao;
import org.grow.loginto.entity.UserInfo;
import org.grow.loginto.exception.JulyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author: xwg
 * @CreateDate: 21-6-7
 */
@RestController
@RequestMapping("/regAbout")
public class RegController {


    @Autowired
    private UserInfoDao userInfoDao;


    @PostMapping("/register")
    public Boolean register(@RequestBody UserInfo userInfo){
        userInfo.setUserId(0);
        userInfo.setLastLoginTime(LocalDateTime.now());
        userInfo.setIsEnable(true);
        Boolean r = this.userExits(userInfo.getUsername());
        if (r) throw new JulyException("用户名已存在无法注册");
        userInfoDao.save(userInfo);
        return true;
    }
    @GetMapping("/usernameExits")
    public Boolean userExits(@RequestParam String username){

        UserInfo results = userInfoDao.findByUsername(username);
        return  results != null ;
    }
}
